package com.joy.ogljoy_01.opengl.ui;

import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.joy.ogljoy_01.opengl.Graphics;
import com.joy.ogljoy_01.opengl.Screen;
import com.joy.ogljoy_01.opengl.Texture2D;
import com.joy.ogljoy_01.opengl.proto.PointF;
import com.joy.ogljoy_01.opengl.proto.RectangleF;
import com.joy.ogljoy_01.opengl.util.BufferUtil;

public class Image {
	private Texture2D texture;
	
	private int mWidth;
	private int mHeight;
	private int mPowWidth;
	private int mPowHeight;
	
	/** 裁剪区域 */
	private RectangleF clipRect;
	
	/** 旋转角度 */
	private float angle;
	/** 绘制锚点 0-1*/
	private PointF anchorPoint;
	
	
	public static Image create(String name) {
		Image img = new Image();
		
		img.init(name);
		
		return img;
	}
	protected void init(String name) {
		texture = Texture2D.create(name);
		mWidth = texture.size.width;
		mHeight = texture.size.height;
		mPowWidth = texture.rect.width;
		mPowHeight = texture.rect.height;
		
		anchorPoint = new PointF(0, 0);
		clipRect = new RectangleF(0, 0, mWidth, mHeight);
	}
	
	/** Our vertex buffer. */
	private FloatBuffer mVerticesBuffer = null;
	/** Our texcoord buffer. */
	private FloatBuffer mTexcoordsBuffer = null;
	/** Our index buffer. */
	private ShortBuffer mIndicesBuffer = BufferUtil.shortBuffer(new short[]{0, 1, 2, 1, 2, 3});
	
	public void draw(float x, float y) {
		draw(x, y, clipRect);
	}
	public void draw(float x, float y, RectangleF clipRect) {
//		 Graphics.gl.glBindTexture(GL10.GL_TEXTURE_2D, texture.texId);
		 texture.bindTexture();
		 Graphics.gl.glPushMatrix();

		 Graphics.gl.glScalef(Screen.SCALE, Screen.SCALE, 1);
		 Graphics.gl.glTranslatef(Screen.OFFSET_X, Screen.OFFSET_Y, 0);
		 Graphics.gl.glTranslatef(x, y, 0);
		 Graphics.gl.glRotatef(angle, 0, 0, 1);
		 // 获取将要绘制的区域和Image本身的剪切区域的交集
		 RectangleF rect = RectangleF.intersection(0, 0, mWidth, mHeight, 
				 clipRect.x, clipRect.y, clipRect.width, clipRect.height);
		 mVerticesBuffer = BufferUtil.getVerticesBuffer(
				 -rect.width*anchorPoint.x, -rect.height*anchorPoint.y, rect.width, rect.height);
		 mTexcoordsBuffer = BufferUtil.getTexcoordsBuffer(rect.x, rect.y, rect.width, rect.height, 
				mPowWidth, mPowHeight);
		 //设置正方体 各顶点
		 Graphics.gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mVerticesBuffer);
		 Graphics.gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTexcoordsBuffer);
//	     Graphics.gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		 Graphics.gl.glDrawElements(GL10.GL_TRIANGLES, mIndicesBuffer.remaining(), 
	    		 GL10.GL_UNSIGNED_SHORT, mIndicesBuffer);
	     
		 Graphics.gl.glPopMatrix();
	}
	
	public int width() {
		return mWidth;
	}
	public int height() {
		return mHeight;
	}
	public int powWidth() {
		return mPowWidth;
	}
	public int powHeight() {
		return mPowHeight;
	}
	
	public void recycle() {
		texture.relase();
	}
	public float getAngle() {
		return angle;
	}
	public void setAngle(float angle) {
		this.angle = angle;
	}
	public PointF getAnchorPoint() {
		return anchorPoint;
	}
	public void setAnchorPoint(PointF anchorPoint) {
		this.anchorPoint = anchorPoint;
	}
}